# from pyttsx3 import speak
# from threading import Lock
#
# lock = Lock()
#
#
# class Broadcast:  # 语音播报
#     @staticmethod
#     def start_broad(broad_data):
#         lock.acquire()  # 获取s资源:all
#         try:
#             speak(broad_data)
#         finally:
#             lock.release()


import RPi.GPIO as GPIO
import time
import os


class Broadcast:  # 蜂鸣器播报
    # 设置蜂鸣器连接的GPIO引脚编号
    # 根据您的实际连接情况修改
    def __init__(self):
        self.BUZZER_PIN = 11
        self.setup()

    def setup(self):
        GPIO.setmode(GPIO.BOARD)  # 设置GPIO引脚编号方式为按物理位置编号
        GPIO.setup(self.BUZZER_PIN, GPIO.OUT)  # 配置GPIO引脚为输出模式，并默认设置为高电平（静音）
        GPIO.output(self.BUZZER_PIN, GPIO.HIGH)  # 确保蜂鸣器在程序开始时不会发声

    def loop(self, t=0.1):
        GPIO.output(self.BUZZER_PIN, GPIO.HIGH)  # 高电平，蜂鸣器停止发声
        time.sleep(0.3)  # 等待0.3秒
        GPIO.output(self.BUZZER_PIN, GPIO.LOW)  # 低电平，蜂鸣器发声
        time.sleep(t)  # 蜂鸣器发声0.1秒后停止
        GPIO.output(self.BUZZER_PIN, GPIO.HIGH)  # 确保蜂鸣器在程序结束时不会发声（虽然这通常是默认的）
        GPIO.cleanup()  # 释放GPIO资源

    @staticmethod
    def speek(text):
        cmdline = f'espeak -vzh+f3 -k10 -s250 "{text}"'
        os.system(cmdline)


if __name__ == '__main__':
    # Broadcast().loop(0.1)
    Broadcast.speek("你好，世界！")
